/*
 *接收控制端（android,linux...）的数据，然后发送给单片机
 *接收单片机数据，然后发送给控制端
 *
*/

#include "serial.h"

#include <QCoreApplication>

#include <QtSerialPort/QSerialPort>
#include <QStringList>
#include <QSettings>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int argumentCount = QCoreApplication::arguments().size();
    QStringList argumentList = QCoreApplication::arguments();

    QTextStream standardOutput(stdout);

    if (argumentCount == 1) {
        //standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]").arg(argumentList.first()) << endl;
        //return 1;
    }

    QSerialPort serialPort;
    //QString serialPortName = argumentList.at(1);
    QString serialPortName = "/dev/ttyUSB0";
    serialPort.setPortName(serialPortName);

    int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud115200;
    serialPort.setBaudRate(serialPortBaudRate);
    serialPort.setDataBits(QSerialPort::Data8);
    serialPort.setParity(QSerialPort::NoParity);
    serialPort.setStopBits(QSerialPort::OneStop);
    serialPort.setFlowControl(QSerialPort::NoFlowControl);

    QSettings set("auxun", "control");
    set.setValue("number", 1);

    if (!serialPort.open(QIODevice::ReadWrite)) {
        standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
        return 1;
    }

    serial m_serial(&serialPort);

    return a.exec();
}
